<template>
  <div class="echart" ref="chartComRef"></div>
</template>

<script>
import { defineComponent, onMounted, watch, ref, shallowRef } from "vue";
import * as echarts from "echarts";
export default defineComponent({
  props: {
    options: {
      type: Object,
      default: {},
      required: true,
    },
    forceRefresh: {
      type: Boolean,
      default: false,
    },
    typemap: {
      type: Boolean,
      default: false,
    },
  },
  emits: ['getMapEchartClick'],
  setup(props, { emit }) {
    const chartComRef = ref(null);
    const chart = shallowRef(null);
    const resizeHandler = () => {
      chart.value.resize();
    };

    // const emit = defineEmits(["getMapEchartClick"]);
    onMounted(() => {
      // if (registerJson) {

      // }
      chart.value = echarts.init(chartComRef.value);
      chart.value.setOption(props.options, true);
      mapclick()
      window.addEventListener("resize", resizeHandler);
    });
    function mapclick() {
      chart.value?.off("click", emitMapClick)
      chart.value.on("click", emitMapClick)
    }
    function emitMapClick(param) {
      emit('getMapEchartClick', param)
    }
    watch(
      () => props.options,
      (newOptions) => {
        chart.value && chart.value.setOption(newOptions, props.forceRefresh);
      },
      {
        deep: true,
      }
    );

    return {
      chartComRef,
      resizeHandler
    };
  },
});
</script>

<style lang="scss" scoped>
.echart {
  width: 100%;
  height: 100%;
}
</style>